/**
 * Manages creates, edits, deletes, and generally handles Users.
 */
package edu.gatech.stolen.tea;

import java.util.Hashtable;
import java.util.Map;

public class UserManager {
	
	private static Map<String, User> users = new Hashtable<String, User>(50);
	private static int population = users.size();
	
	public UserManager() {
		
	}
	
	public static boolean login (String username, String password) {
		
		if (contains(username)) {
			if (users.get(username).getPassword().equals(password)) {
				return true;
			}
		}
		
		return false;
		
	}
	
	/**
	 * creates a new user and saves it
	 */
	public static boolean createUser(String first, String last, String username, String password, String mail) {
		
		if (!contains(username)){
			User user = new User();
			user.setFirstName(first);
			user.setLastName(last);
			user.setUser(username);
			user.setPassword(username);
			user.setMail(mail);
			users.put(username, user);
			setPopulation(getPopulation() + 1);
			return true;
		}
		
		return false;
		
	}
	
	/**
	 * edits a user based on the criteria given to the method in the what variable
	 */
	public static boolean editUser(String username, editUserEnum what, String data) {
		
		if (contains(username)){
			what.edit(username, data);
			return true;
		}
		
		return false;
		
	}
	
	/**
	 * deletes a user
	 */
	public static boolean deleteUser(String username) {
		
		if (contains(username)){
			setPopulation(getPopulation() - 1);
			users.remove(username);
			return true;
		}
		
		return false;
		
	}
	
	/**
	 * checks to see if a user exists based on its username
	 */
	public static boolean contains(String username) {
		
		return users.containsKey(username);
		
	}
	
	/**
	 * gets a list of usernames
	 */
	public static Map<String, User> getUsers() {
		
		return users;
		
	}
	
	/**
	 * gets user
	 */
	public static User getUser(String username) {
		
		if (contains(username)){
			return users.get(username);
		}
		
		return null;
		
	}
	
	/**
	 * gets the number of users that are registered when the method is called
	 */
	public static int getPopulation() {
		
		return population;
		
	}

	public static void setPopulation(int population) {
		
		UserManager.population = population;
		
	}
	
}
